package com.xyx.yfy_site_msg.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyx.yfy_site_msg.system.entity.YfyCustomer;
import com.xyx.yfy_site_msg.system.entity.YfyDataEntity;
import com.xyx.yfy_site_msg.system.entity.YfySite;
import com.xyx.yfy_site_msg.system.entity.YfyUser;
import com.xyx.yfy_site_msg.system.service.YfyCustomerService;
import com.xyx.yfy_site_msg.system.service.YfySiteService;
import com.xyx.yfy_site_msg.system.service.YfySystemService;
import com.xyx.yfy_site_msg.system.utils.Constast;
import com.xyx.yfy_site_msg.system.utils.DataGridView;
import com.xyx.yfy_site_msg.system.utils.ResultObj;
import com.xyx.yfy_site_msg.system.utils.WebUtils;
import com.xyx.yfy_site_msg.system.vo.YfySiteVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyx
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/site")
public class YfySiteController {

    private static final Integer USER_TYPE = 0;
    @Autowired
    private YfySiteService siteService;
    @Autowired
    private YfyCustomerService customerService;
    @Autowired
    private YfySystemService systemService;

    /*
     * 查询所有站点数据+模糊查询
     * */
    @RequestMapping("loadAllSite")
    public DataGridView loadAllSite(YfySiteVo siteVo) {
        IPage<YfySite> page = new Page<>(siteVo.getPage(), siteVo.getLimit());
        QueryWrapper<YfySite> queryWrapper = new QueryWrapper<>();
        YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
        if (user.getUsertype() == USER_TYPE) {
            List<YfySite> list = this.siteService.loadSiteByUserType();
            return new DataGridView(list);
        } else {
            queryWrapper.like(StringUtils.isNotBlank(siteVo.getCustomer()), "S_Customer", siteVo.getCustomer());
            queryWrapper.like(StringUtils.isNotBlank(siteVo.getSitename()), "S_SiteName", siteVo.getSitename());
            queryWrapper.like(StringUtils.isNotBlank(siteVo.getTelephone()), "S_Telephone", siteVo.getTelephone());
            queryWrapper.like(StringUtils.isNotBlank(siteVo.getSubmitter()), "S_Submitter", siteVo.getSubmitter());
            queryWrapper.ge(siteVo.getStartTime() != null, "S_SubmissionTime", siteVo.getStartTime());
            queryWrapper.le(siteVo.getEndTime() != null, "S_SubmissionTime", siteVo.getEndTime());
            queryWrapper.ge(siteVo.getStartUpdateTime() != null, "S_Updatetime", siteVo.getStartUpdateTime());
            queryWrapper.le(siteVo.getEndUpdateTime() != null, "S_Updatetime", siteVo.getEndUpdateTime());
            queryWrapper.orderByDesc("ID");
            try {
                this.siteService.page(page, queryWrapper);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new DataGridView(page.getTotal(), page.getRecords());
        }
    }

    /**
     * 根据客户名称分页查询名下所有站点
     *
     * @param siteVo
     * @return
     */
    @RequestMapping("loadSiteByCustomer")
    public DataGridView loadSiteByCustomer(YfySiteVo siteVo) {
        IPage<YfySite> page = new Page<>(siteVo.getPage(), siteVo.getLimit());
        QueryWrapper<YfySite> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(siteVo.getCustomer()), "S_Customer", siteVo.getCustomer());
        queryWrapper.orderByDesc("ID");
        try {
            this.siteService.page(page, queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new DataGridView(page.getTotal(), page.getRecords());

    }

    /*
     * 添加站点数据
     * */
    @RequestMapping("addSite")
    public ResultObj addSite(YfySiteVo siteVo) {
        try {
            YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
//            Integer customerid = Integer.valueOf(siteVo.getCustomer());
//            String customer=this.customerService.loadCustomerNameById(customerid);
//            siteVo.setCustomer(customer);
            String deploymentid = siteVo.getDeployment();
            String deployment = this.systemService.loadDeploymentById(deploymentid);
            siteVo.setDeployment(deployment);
            String sourceofcustomerid = siteVo.getSourceofcustomer();
            String sourceofcustomer = this.systemService.loadSourceOfCustomerById(sourceofcustomerid);
            siteVo.setSourceofcustomer(sourceofcustomer);
            if (sourceofcustomer == null) {
                sourceofcustomer = "受理";
                siteVo.setSourceofcustomer(sourceofcustomer);
            }
            siteVo.setSubmitter(user.getUsername());
            siteVo.setSubmissiontime(new Date());
            this.siteService.save(siteVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /*
     * 修改站点数据
     * */
    @RequestMapping("updateSite")
    public ResultObj updateSite(YfySiteVo siteVo) {
        try {
            YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
            siteVo.setUpdatebody(user.getUsername());
            siteVo.setUpdatetime(new Date());
//            Integer customerid = Integer.valueOf(siteVo.getCustomer());
//            String customer=this.customerService.loadCustomerNameById(customerid);
//            siteVo.setCustomer(customer);
            String deploymentid = siteVo.getDeployment();
            String deployment = this.systemService.loadDeploymentById(deploymentid);
            siteVo.setDeployment(deployment);
            String sourceofcustomerid = siteVo.getSourceofcustomer();
            String sourceofcustomer = this.systemService.loadSourceOfCustomerById(sourceofcustomerid);
            siteVo.setSourceofcustomer(sourceofcustomer);
            this.siteService.updateById(siteVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /*
     * 删除站点数据
     * */
    @RequestMapping("deleteSite")
    public ResultObj deleteSite(Integer id) {
        try {
            this.siteService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_SUCCESS;
        }
    }

    /*
     * 批量删除站点数据
     * */
    @RequestMapping("batchDeleteSite")
    public ResultObj batchDeleteSite(YfySiteVo siteVo) {
        try {
            Collection<Serializable> idList = new ArrayList<>();
            for (Integer id : siteVo.getIds()) {
                idList.add(id);
            }
            this.siteService.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /*
     * 查询站点档案提交数据
     * */
    @RequestMapping("loadSubmit")
    @ResponseBody
    public Map<String, Object> loadSubmit(String month) {
        List<YfyDataEntity> list = this.siteService.loadSubmit(month);
        HashMap<String, Object> map = new HashMap<>();
        List<String> names = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for (YfyDataEntity dataEntity : list) {
            names.add(dataEntity.getName());
            values.add(dataEntity.getValue());
        }
        map.put("name", names);
        map.put("value", values);
        return map;
    }

    /*
     * 查询站点档案更新数据
     * */
    @RequestMapping("loadupdate")
    @ResponseBody
    public Map<String, Object> loadupdate(String month) {
        List<YfyDataEntity> list = this.siteService.loadUpdate(month);
        HashMap<String, Object> map = new HashMap<>();
        List<String> names = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for (YfyDataEntity dataEntity : list) {
            names.add(dataEntity.getName());
            values.add(dataEntity.getValue());
        }
        map.put("name", names);
        map.put("value", values);
        return map;
    }
}
